package br.ita.escolaita.ejb;

import br.ita.escolaita.model.entity.Aluno;
import java.text.SimpleDateFormat;
import br.ita.escolaita.model.entity.Turma;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import static br.ita.escolaita.util.EscolaItaUtil.isBlankOrNull;

@Stateless
public class AlunoEjbBean implements AlunoEjbBeanLocal {

    @PersistenceContext(unitName = "escolaIta-ejbPU")
    private EntityManager em;

    public void salvar(Aluno aluno) {
        em.persist(aluno);
        em.flush();
    }

    public void alterar(Aluno aluno) {
        em.merge(aluno);
        em.flush();
    }

    public List<Aluno> listarTodos() {
        Query findAllQuery = em.createNamedQuery("aluno.findAll");
        List<Aluno> alunos = findAllQuery.getResultList();
        return alunos;
    }

    public List<Aluno> listar(Aluno a) {
        StringBuilder sql = new StringBuilder();
        StringBuilder where = new StringBuilder();
        sql.append("SELECT a FROM Aluno a ");
        if (!isBlankOrNull(a.getNome())) {
            where.append("WHERE UPPER(a.nome) LIKE UPPER('%").append(a.getNome()).append("%') ");
        }
        if (a.getSexo() != null){
            if (where.length() != 0){
                where.append("AND ");
            } else {
                where.append("WHERE ");
            }
            where.append("a.sexo = '").append(a.getSexo()).append("' ");
        }
        if (a.getDataNascimento() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            if (where.length() != 0){
                where.append("AND ");
            } else {
                where.append("WHERE ");
            }
            where.append("a.dataNascimento = '").append(sdf.format(a.getDataNascimento())).append("' ");
        }
        if (!isBlankOrNull(a.getCpf())){
            if (where.length() != 0){
                where.append("AND ");
            } else {
                where.append("WHERE ");
            }
            where.append("a.cpf = '").append(a  .getCpf()).append("' ");
        }
        if (!isBlankOrNull(a.getLogradouro())){
            if (where.length() != 0){
                where.append("AND ");
            } else {
                where.append("WHERE ");
            }
            where.append("UPPER(a.logradouro) LIKE UPPER('").append(a.getCpf()).append("') ");
        }
        if (a.getNumero() != 0){
            if (where.length() != 0){
                where.append("AND ");
            } else {
                where.append("WHERE ");
            }
            where.append("a.numero = ").append(a.getNumero()).append(" ");
        }
        if (!isBlankOrNull(a.getCep())){
            if (where.length() != 0){
                where.append("AND ");
            } else {
                where.append("WHERE ");
            }
            where.append("a.cep = '").append(a.getCep()).append("' ");
        }
        if (!isBlankOrNull(a.getComplemento())){
            if (where.length() != 0){
                where.append("AND ");
            } else {
                where.append("WHERE ");
            }
            where.append("UPPER(a.complemento) LIKE UPPER('").append(a.getComplemento()).append("') ");
        }        
        Query findAllQuery = em.createQuery(sql.append(where).toString());
        List<Aluno> alunos = findAllQuery.getResultList();        
        return alunos;
    }

    public List<Aluno> findByTurma(Turma t){
        if (t != null){
            Query q = em.createQuery("SELECT a FROM Aluno a WHERE a.turma =:turma");
            q.setParameter("turma", t);
            return q.getResultList();
        }
        return null;
    }
}
